/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onurkagan.ksnack.slice;

import com.onurkagan.ksnack.ResourceTable;
import com.onurkagan.ksnack_lib.Animations.Fade;
import com.onurkagan.ksnack_lib.Animations.Slide;
import com.onurkagan.ksnack_lib.KSnack.KSnack;
import com.onurkagan.ksnack_lib.MinimalKSnack.MinimalKSnack;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;

/**
 * 主要能力片
 *
 * @author dev
 * @since 2021-07-08
 */
public class MainAbilitySlice extends AbilitySlice {
    /**
     * btn显示
     */
    private Button btnShow;
    /**
     * btn驳回
     */
    private Button btnDismiss;
    /**
     * ksnack
     */
    private KSnack ksnack;
    /**
     * 最小ksnack
     */
    private MinimalKSnack minimalKsnack;
    /**
     * 根
     */
    private ComponentContainer root;
    /**
     * 持有人
     */
    private StackLayout holder;

    /**
     * 在开始
     *
     * @param intent 意图
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // Initialize objects.
        root = (ComponentContainer) findComponentById(ResourceTable.Id_root);
        minimalKsnack = new MinimalKSnack(root);
        ksnack = new KSnack(root);
        btnShow = (Button) findComponentById(ResourceTable.Id_activity_main_btn_show);
        btnDismiss = (Button) findComponentById(ResourceTable.Id_activity_main_btn_dissmis);
        holder = (StackLayout) findComponentById(ResourceTable.Id_activity_main_lyt_holder);
        getWindow().setStatusBarColor(Color.getIntColor("#0093FF"));
        initFraction();
        btnShow.setClickedListener(new Component.ClickedListener() {
            public void onClick(Component component) {
                // Minimal KSnack
                minimalKsnack
                    .setMessage("This is minimal KSnack !")
                    .setAnimation(Fade.In.getAnimation(minimalKsnack.snackView),
                        Fade.Out.getAnimation(minimalKsnack.snackView))
                    .show();

                // KSnack
                ksnack
                    .setMessage("This is KSnack !")
                    .setAnimation(Slide.Up.getAnimation(root, ksnack.rlvHost),
                        Slide.Down.getAnimation(root, ksnack.rlvHost))
                    .show();
            }
        });

        btnDismiss.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                minimalKsnack.dismiss();
                if (minimalKsnack.snackView.getVisibility() == Component.VISIBLE) {
                    ksnack.dismiss();
                }
            }
        });
    }

    /**
     * 初始化部分
     */
    private void initFraction() {
        ((FractionAbility) getAbility()).getFractionManager()
            .startFractionScheduler()
            .add(ResourceTable.Id_activity_main_lyt_holder, new BlankFraction())
            .submit();
    }

    /**
     * 在活跃的
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * 在前台
     *
     * @param intent 意图
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
